<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登陆</a>
        <a href="javascript:logout()">退出登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="artCount">2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right" id="artDiv">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->

        </div>
    </div>
</body>
<script>
    function showInfo()
    {
        jQuery.ajax(
            {
                url:"/user/showinfo",
                type:"POST",
                data:{},
                success:function (result) {
                    if(result != null && result.code == 200)
                    {
                        jQuery("#username").text(result.data.username);
                        jQuery("#artCount").text(result.data.artCount);
                    }else
                    {
                        alert("获取个人信息失败，请重新登陆"); //session可能过期？
                    }
                }
            }
        );
    }
    showInfo();

    function getMyArtlist()
    {
        jQuery.ajax(
            {
                url:"/art/mylist",
                type:"POST",
                data:{},
                success: function (result) {
                    var ArtList = "";
                    if(result != null && result.code == 200)
                    {
                        if(result.data != null && result.data.length > 0)
                        {
                            for(var i = 0 ; i < result.data.length ; i++)
                            {
                                var ArtItem = result.data[i];
                                ArtList += '<div class="blog">';
                                ArtList += '<div class="title">'+ArtItem.title+ '</div>';
                                ArtList += '<div class="date">'+ArtItem.createtime+ '</div>';
                                ArtList += '<div class="desc">';
                                ArtList += ArtItem.content;
                                ArtList += '</div>';

                                ArtList += '<a href="blog_content.html?id='
                                    +ArtItem.id+'" class="detail">查看全文 &gt;&gt;</a>'
                                ArtList += "&ensp;&ensp;"
                                ArtList += '<a href="blog_update.html?id='+ArtItem.id+'" class="detail">修改 &gt;&gt;</a>'
                                ArtList += "&ensp;&ensp;"

                                ArtList += '<a href="javascript:mydel('
                                    +ArtItem.id+');" class="detail">删除 &gt;&gt;</a>'

                                ArtList += '</div>';
                                jQuery("#artDiv").html(ArtList);
                            }

                        }else
                        {
                            //还没有发表文章
                            jQuery("#artDiv").html("<h3>暂无发表文章</h3>");
                        }

                    }else
                    {
                        alert("你的登陆已过期，请重新登陆");
                    }
                }
            }
        );
    }
    getMyArtlist();

    function mydel(id)
    {
        if(confirm("你确定要删除该文章吗？")) {
            jQuery.ajax(
                {
                    url: "/art/del",
                    type: "POST",
                    data: {"id": id},
                    success: function (result) {
                        if (result != null && result.code == 200 && result.data == 1) {
                            alert("删除成功！");
                            location.href = location.href;
                        } else {
                            alert("登陆已失效，请重新登陆..");
                        }
                    }
                }
            );
        }
    }

    function logout() {
        if(confirm("请确认是否退出？"))
        {
            jQuery.ajax(
                {
                    url:"user/logout",
                    type:"POST",
                    data:{},
                    success:function(result) {
                        if(result != null && result.code == 200)
                            location.href = "/login.html";
                        else   location.href = location.href;

                    }
                }
            );
        }
    }
</script>
</html>